<template>
  <div>
    <!-- 最终使用版：商品点单列表 -->
    <!-- 头部 -->
    <div class="header">
      <div class="left">
        <router-link to="/">
          <img src="/xrzp/imgs/left.svg" height="20px" width="20px" />
          返回
        </router-link>
      </div>
      <router-link to="/store" class="right">
        <!-- <span>欢迎，{{$store.state.userId}}</span> -->
        <span>{{ addressInfoNow.title }}</span>
        <div class="pic">
          <img src="/xrzp/imgs/login1.png" alt="" />
        </div>
      </router-link>
    </div>

    <!-- 搜索框 -->
    <div class="search-wrap">
      <div class="search">
        <div class="search-icon"></div>
        <input type="text" value="" v-model="value" placeholder="有机青菜粥" />
      </div>
    </div>

    <!-- 商品内容部分 -->
    <!-- 商品类别 -->
    <div class="content">
      <div class="left">
        <ul>
          <li
            v-for="(item, i) of categoryName"
            :key="i"
            @click="changeCategory(item.scId)"
            :class="{ click: categoryId == i + 1 }"
          >
            <img :src="`/xrzp/imgs/categoryName${item.scId}.svg`" alt="" />
            <p>{{ item.categoryName }}</p>
          </li>
        </ul>
      </div>

      <!-- 商品列表 -->
      <div class="right">
        <ul>
          <li v-for="(item, i) of shopList" :key="i" data-i="item.shopId">
            <img :src="item.coverImg" />
            <div class="text">
              <h5>{{ item.shopName }}</h5>
              <h6>{{ item.shopIntroduction | shopIntrFilter }}</h6>
              <span>￥{{ item.originalPrice.toFixed(2) }}</span>
            </div>

            <!-- 商品数量加减子组件 -->
            <div class="add">
              <cart-control
                :ref="`dataNum[${i}]`"
                :select-food="selectFood"
                :data-i="`${i}`"
                @change-count="changeCount($event, i)"
              >
              </cart-control>
            </div>
          </li>
        </ul>
      </div>
    </div>

    <!-- 商品详情 -->

    <!-- 购物车子组件,将被选中的商品信息传递给购物车子组件 -->
    <shop-cart :select-food="selectFood"></shop-cart>

    <!-- 底部导航栏 -->
    <mt-tabbar v-model="selected" fixed>
      <mt-tab-item id="home">
        <img slot="icon" src="/imgs/shouye.svg" v-if="selected == 'home'" />
        <img slot="icon" src="/imgs/shouye1.svg" v-else />
        <span :class="selected == 'home' ? 'select' : 'noselect'">首页</span>
      </mt-tab-item>
      <mt-tab-item id="productList">
        <img
          slot="icon"
          src="/imgs/diandan.svg"
          v-if="selected == 'productList'"
        />
        <img slot="icon" src="/imgs/diandan2.svg" v-else />
        <span :class="selected == 'productList' ? 'select' : 'noselect'"
          >点单</span
        >
      </mt-tab-item>
      <mt-tab-item id="order">
        <img slot="icon" src="/imgs/dingdan.svg" v-if="selected == 'order'" />
        <img slot="icon" src="/imgs/dingdan1.svg" v-else />
        <span :class="selected == 'order' ? 'select' : 'noselect'">订单</span>
      </mt-tab-item>
      <mt-tab-item id="my">
        <img slot="icon" src="/imgs/wode.svg" v-if="selected == 'my'" />
        <img slot="icon" src="/imgs/wode1.svg" v-else />
        <span :class="selected == 'my' ? 'select' : 'noselect'">我的</span>
      </mt-tab-item>
    </mt-tabbar>
  </div>
</template>


<script>
// 引入购物车数量加减子组件
import CartControl from "./CartControl.vue";
// 引入购物车子组件
import ShopCart from "./ShopCart.vue";
// 引入vuex
import { mapState } from "vuex";

export default {
  name: "goods",
  components: { CartControl, ShopCart },
  data() {
    return {
      selected: "productList", //底部导航选项
      value: "", //搜索框内容
      categoryName: [], //商品类别
      categoryId: "1", // 商品类别 Id,设置初始值为1
      shopList: {}, // 商品列表
      selectFood: [
        {
          coverImg:
            "https://xrzp-file.oss-cn-shanghai.aliyuncs.com/admin/shopImg/1001.jpg",
          originalPrice: 15,
          scId: 1,
          shopId: 20,
          shopIntroduction:
            "有机青菜粥是一道以青菜、米饭作为主要食材，以盐、鸡精、香油作为辅料制作而成的美食，口味咸，具有烹饪简单又营养美味的特点",
          shopName: "有机青菜粥",
          count: 2
        },
        {
          coverImg:
            "https://xrzp-file.oss-cn-shanghai.aliyuncs.com/admin/shopImg/1001.jpg",
          originalPrice: 20,
          scId: 2,
          shopId: 20,
          shopIntroduction:
            "有机青菜粥是一道以青菜、米饭作为主要食材，以盐、鸡精、香油作为辅料制作而成的美食，口味咸，具有烹饪简单又营养美味的特点",
          shopName: "有机青菜粥",
          count: 2
        },
        {
          coverImg:
            "https://xrzp-file.oss-cn-shanghai.aliyuncs.com/admin/shopImg/1001.jpg",
          originalPrice: 25,
          scId: 1,
          shopId: 20,
          shopIntroduction:
            "有机青菜粥是一道以青菜、米饭作为主要食材，以盐、鸡精、香油作为辅料制作而成的美食，口味咸，具有烹饪简单又营养美味的特点",
          shopName: "有机青菜粥",
          count: 3
        }
      ],
      spId: 1 //当前商品id
    };
  },
  // 定义过滤器
  filters: {
    shopIntrFilter(oldVal) {
      if (oldVal.length > 22) {
        return oldVal.slice(0, 22) + "...";
      }
    }
  },
  methods: {
    // 商品数量变更
    changeCount(val) {
      // console.log(this.$refs.dataNum);
      if (val.type == "add") {
        this.selectFood.some(item => {
          // 商品数量 +1
          item.count++;
          return true;
        });
      } else if (val.type == "mimus") {
        this.selectFood.some(item => {
          // 商品数量 -1
          item.count--;
          return true;
        });
      }
    },

    // 封装一个请求 商品分类 数据的函数
    getShopCategory() {
      this.axios.get("/api/shop/getShopCategory").then(result => {
        // console.log(result.data.data);
        this.categoryName = result.data.data;
      });
    },

    // 封装一个 根据商品分类的scId 来请求其对应分类的商品 数据的函数
    getShopByCategory() {
      this.axios
        .get("/api/shop/getShopByCategory", {
          params: { categoryId: this.categoryId }
        })
        .then(result => {
          // console.log(result.data.data);
          this.shopList = result.data.data;
        });
    },

    // 点击商品分类时，查询对应的商品信息
    changeCategory(categoryId) {
      this.categoryId = categoryId;
      // console.log(categoryId);
      // console.log(this.categoryId);
      this.getShopByCategory();
    }
  },
  // 结构变量
  computed: {
    ...mapState(["addressInfoNow"]) //获取店铺名称
  },
  watch: {
    //监听 selected 值，当 selected 变化时，跳转到相应的界面
    selected(newval) {
      if (newval == "home") {
        this.$router.push("/");
      } else if (newval == "order") {
        this.$router.push("/order");
      } else if (newval == "my") {
        this.$router.push("/my");
      }
    }
  },
  mounted() {
    // 页面挂载时，请求商品类别
    this.getShopCategory();
    // 页面挂载时，请求商品类别 scId 为 1 的商品信息
    this.getShopByCategory();
  }
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
/* 头部 */
.header {
  /* position: relative; */
  position: fixed;
  z-index: 200;
  top: 0;
  width: 100%;
  height: 40px;
  background-color: white;
}
.header .left {
  position: absolute;
  width: 20%;
  line-height: 40px;
  padding-left: 10px;
}
a {
  text-decoration: none;
  color: #8a8a8a;
  font-size: 17px;
}
.header .left img {
  vertical-align: center;
  height: 17px;
}
.header .right {
  position: absolute;
  line-height: 36px;
  font-size: 14px;
  right: 0;
}
.header .right .pic {
  display: inline-block;
  height: 36px;
  background-color: white;
  border-radius: 50%;
  margin: 2px 5px;
}
.header .right .pic img {
  height: 36px;
}

/* 搜索框部分 */
.search-wrap {
  /* position: relative;*/
  position: fixed;
  top: 40px;
  z-index: 100;
  width: 100%;
  height: 40px;
  /* border: 2px solid #ff8c00; */
  border-bottom: 1px solid #d6d6d6;
  background-color: #fff;
  /* padding: 2px 5px; */
  /* border-radius: 15px; */
}
.search-wrap > .search {
  position: absolute;
  border: 2px solid #ff8c00;
  border-radius: 15px;
  width: 98%;
  height: 30px;
  margin: 3px 5px;
  overflow: hidden;
}
.search-wrap .search-icon {
  position: absolute;
  height: 20px;
  width: 20px;
  background: url(/imgs/search.png) no-repeat;
  background-size: 20px 20px;
  top: 3px;
  left: 5px;
}
.search-icon::after {
  content: "";
  display: block;
  height: 16px;
  border: 1px solid #cccccc;
  position: absolute;
  right: -8px;
  top: 2px;
}
.search-wrap input {
  height: 26px;
  width: 100%;
  margin-left: 42px;
  border: none;
}
.search-wrap input::placeholder {
  color: #a9a9a9;
  font-size: 14px;
}

/* 商品内容部分 */
/* 左侧导航部分 */
.content {
  position: relative;
  top: 75px;
  /* background-color: greenyellow; */
  margin-top: 5px;
}
.content .left {
  position: fixed;
  overflow-y: scroll;
  /* float: left; */
  left: 0;
  top: 80px;
  bottom: 55px;
  width: 25%;
  /* background-color: #ffd700; */
}
/* 商品分类 */
.content .left ul li {
  display: block;
  width: 100%;
  height: 80px;
  /* border-bottom: 1px solid #d6d6d6; */
  text-align: center;
  /* line-height: 35px; */
}
/* 点击时商品类别时的样式变化 */
.click {
  border-left: 3px solid #ff8c00;
}
/* 商品类别图标 */
.content .left ul li img {
  height: 40px;
  margin-top: 5px;
}

/* 右侧商品部分 */
.content .right {
  position: fixed;
  z-index: -1;
  right: 0;
  overflow-y: scroll;
  top: 80px;
  bottom: 55px;
}
.content .right ul li {
  float: right;
  position: relative;
  /* background-color: greenyellow; */
  padding: 5px;
  height: 90px;
  border-left: 1px solid #e6e6e6;
  /* border-bottom: 1px solid #e6e6e6; */
  width: 75%;
  margin: 2px 0;
}
.content .right img {
  height: 80px;
  width: 80px;
  background-color: #fff;
}
.content .right .text {
  height: 80px;
  position: absolute;
  left: 95px;
  top: 5px;
  font-family: "Avenir, Helvetica, Arial, sans-serif";
}
.content .right .text h5 {
  /* font-weight: 700; */
  font-size: 17px;
}
.content .right .text h6 {
  font-size: 12px;
  margin-top: 5px;
  color: #868686;
}
.content .right .text span {
  font-size: 10px;
  color: #ff4d4d;
  font-family: "Microsoft YaHei";
  position: absolute;
  bottom: 5px;
}
.content .right .add {
  position: absolute;
  right: 12px;
  bottom: 10px;
}

/* 底部导航 */
.select {
  color: black;
  font-size: 12px;
}
.noselect {
  color: #aaaaaa;
  font-size: 12px;
}
</style>